using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SharePointAnalyzer;


namespace SPAnalyzer
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                string url = "http://"+Environment.MachineName;
                Console.WriteLine("Input URL (default={0}):", url);
                
                string url2=Console.ReadLine();
                if (url2.Length > 0)
                    url = url2;
                Uri uri = new Uri(url);
                XmlWriter writer = null;
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.IndentChars = ("\t");
                settings.ConformanceLevel = ConformanceLevel.Fragment;
                
                writer = XmlWriter.Create("data.xml", settings);

                writer.WriteStartElement("data");
                writer.WriteStartElement("webs");
                Hashtable htLists = new Hashtable();
                Hashtable htWebs = new Hashtable();
                Analyzer analyzer = new Analyzer(writer, htLists, htWebs);
                if (uri.Segments.GetLength(0) > 1)
                {
                    using (SPSite site = new SPSite(uri.OriginalString))
                    {
                            SPWeb web = site.RootWeb;
                            analyzer.ProcessWeb(web);
                    }
                }
                else
                {

                    using (SPGlobalAdmin globalAdmin = new SPGlobalAdmin())
                    {
                        SPVirtualServer virtualServer = globalAdmin.OpenVirtualServer(uri);

                        foreach (SPSite site in virtualServer.Sites)
                        {
                            SPWeb web = site.RootWeb;
                            analyzer.ProcessWeb(web);

                        }
                    }
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.Flush();
                writer.Close();
                using (StreamWriter sw = new StreamWriter("lists.csv"))
                {
                    sw.WriteLine("\"Listname\", \"Template\", \"Nr of Instances\", \"Nr of Items\"");
                    foreach (string key in htLists.Keys)
                    {
                        ListInfo li = (ListInfo)htLists[key];

                        Console.WriteLine("Listname {0}, Template {1}, Nr of Instances {2}, Nr of Items {3}", li.listName, key, li.instanceCount, li.itemCount);
                        sw.WriteLine("\"{0}\",\"{1}\",{2}", li.listName, key, li.instanceCount, li.itemCount);
                    }
                    sw.Flush();
                    sw.Close();
                }
                using (StreamWriter sw = new StreamWriter("webs.csv"))
                {
                    sw.WriteLine("WebTemplate, \"Nr of Instances\"");
                    foreach (string key in htWebs.Keys)
                {
                    WebInfo wi = (WebInfo)htWebs[key];

                    Console.WriteLine("Template {0}, Nr of Instances {1}",  key, wi.instanceCount);
                    sw.WriteLine("\"{0}\",{1}", key, wi.instanceCount);
                }
                sw.Flush();
                sw.Close();
                }

            }
            catch (Exception Ex)
            {
                Console.WriteLine("Exception occurred: " + Ex.Message);
            }
            Console.ReadLine();

        }
        

    }
}
